package com.ruoyi.consumer.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.consumer.domain.CCompany;
import com.ruoyi.consumer.domain.UserRole;
import com.ruoyi.consumer.service.ICCompanyService;
import com.ruoyi.consumer.service.ISysUserRoleService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.impl.SysDeptServiceImpl;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 公司Controller
 *
 * @author ruoyi
 * @date 2023-10-11
 */
@RestController
@RequestMapping("/consumer/company")
public class CCompanyController extends BaseController {
    @Autowired
    private ICCompanyService cCompanyService;

    @Autowired
    private SysDeptServiceImpl deptService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysUserRoleService sysUserRoleService;


    /**
     * 查询公司列表
     */
    @PreAuthorize("@ss.hasPermi('consumer:company:list')")
    @GetMapping("/list")
    public TableDataInfo list(CCompany cCompany) {
        Long userAdminId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userAdminId);
        Assert.notNull(sysUser, "用户不存在");

        if (userAdminId != 1) {

            //判断是否是管理员
            UserRole sysUserRole = sysUserRoleService.selectSysUserRoleByUserId(userAdminId);
            if (sysUserRole.getRoleId() == 104) {
                startPage();
                List<CCompany> list = cCompanyService.selectCCompanyList(cCompany);
                return getDataTable(list);
            } else {

                CCompany company = new CCompany();
                company.setCompanDept(Math.toIntExact(sysUser.getDeptId()));
                List<CCompany> cCompanyList = cCompanyService.selectCCompanyList(company);
                if (cCompanyList.isEmpty()) {
                    return null;
                } else {
                    cCompany.setCompanDept(Math.toIntExact(sysUser.getDeptId()));
                }
            }
        }
        startPage();
        List<CCompany> list = cCompanyService.selectCCompanyList(cCompany);
        return getDataTable(list);
    }

    /**
     * 导出公司列表
     */
    @PreAuthorize("@ss.hasPermi('consumer:company:export')")
    @Log(title = "公司", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CCompany cCompany) {
        List<CCompany> list = cCompanyService.selectCCompanyList(cCompany);
        ExcelUtil<CCompany> util = new ExcelUtil<CCompany>(CCompany.class);
        util.exportExcel(response, list, "公司数据");
    }

    /**
     * 获取公司详细信息
     */
    @PreAuthorize("@ss.hasPermi('consumer:company:query')")
    @GetMapping(value = "/{companyId}")
    public AjaxResult getInfo(@PathVariable("companyId") Integer companyId) {
        return success(cCompanyService.selectCCompanyByCompanyId(companyId));
    }

    /**
     * 新增公司
     */
    @PreAuthorize("@ss.hasPermi('consumer:company:add')")
    @Log(title = "公司", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CCompany cCompany) {
        return toAjax(cCompanyService.insertCCompany(cCompany));
    }

    /**
     * 修改公司
     */
    @PreAuthorize("@ss.hasPermi('consumer:company:edit')")
    @Log(title = "公司", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CCompany cCompany) {
        if (cCompany.getStatus() == 3) {
            System.out.println("第一次修改");
            CCompany company = new CCompany();
            company.setRegistrationNumber(cCompany.getRegistrationNumber());
            if (cCompanyService.selectCCompanyList(company).size() > 0) {
                return AjaxResult.error("纳税人识别号不能重复！");
            }
            cCompany.setStatus(0);
        }
        //同步修改对应公司
        SysDept dept = deptService.selectDeptById(Long.valueOf(cCompany.getCompanDept()));
        dept.setDeptName(cCompany.getCompanyName());
        deptService.updateDept(dept);
        return toAjax(cCompanyService.updateCCompany(cCompany));
    }

    /**
     * 删除公司
     */
    @PreAuthorize("@ss.hasPermi('consumer:company:remove')")
    @Log(title = "公司", businessType = BusinessType.DELETE)
    @DeleteMapping("/{companyIds}")
    public AjaxResult remove(@PathVariable Integer[] companyIds) {
        return toAjax(cCompanyService.deleteCCompanyByCompanyIds(companyIds));
    }
}
